package com.mpsc.fbpm.consumer.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author yangzyh
 * @date 2021/6/2 11:13
 */
public class TaskHelper {

    public static void main(String[] args) {
        String businessKey;
        Set<String> businessKeys = new HashSet<>();
//        FileReader reader = new FileReader("D:\\bk.txt");
//        BufferedReader bf = new BufferedReader(reader);
//        while ((businessKey = bf.readLine()) != null) {
//            businessKeys.add(businessKey);
//        }112004
//         360100000
        String businessKeyss =

                "3404110827085947491,3404107571419971780";

        businessKeys = TaskHelper.getBusinessKeys(businessKeyss);
        JSONObject startProcessReq = TaskHelper.makeStartProcessParam(businessKeys, "112004", "361209000", "");
        String  ret = JSON.toJSONString(startProcessReq, SerializerFeature.DisableCircularReferenceDetect);
        System.out.println(ret);
    }

    /**
     * 获取单据ID
     * */
    public static Set<String> getBusinessKeys(String businessKeys){
        String[] businessKeyArry = businessKeys.split(",");
        Set<String> businessKeySet = new HashSet<String>(Arrays.asList(businessKeyArry));
        return businessKeySet;
    }

    /**
     * 启动流程入参
     * @param businessKeys
     * @param bizTypeCode
     * @param rgCode
     * @return
     */
    public static JSONObject makeStartProcessParam(Collection<String> businessKeys, String bizTypeCode, String rgCode, String variable) {
        // 组装入参
        JSONObject req = new JSONObject();
        req.put("userCode","auto_361209000");
        JSONArray paramArray = new JSONArray();
        for(String businessKey: businessKeys) {
            JSONObject paramObj = new JSONObject();
            paramObj.put("bizTypeCode", bizTypeCode);
            paramObj.put("rgCode", rgCode);
            paramObj.put("businessKey", businessKey);
            JSONArray varArray = new JSONArray();
            JSONObject var1 = new JSONObject();
            var1.put("name","menuCode");
            var1.put("type","string");
            var1.put("value", "00111502003" );
            varArray.add(var1);
            paramObj.put("variables", varArray);
            paramArray.add(paramObj);
        }
        req.put("batchProcessInstanceCreateRequest", paramArray);

        return req;
    }

    /**
     * 启动流程入参
     * @param businessKeys
     * @return
     */
    public  JSONObject  makeStartProcessParam(Collection<String> businessKeys) {
        // 组装入参
        JSONObject req = new JSONObject();
        req.put("userCode","999800002");
        JSONArray paramArray = new JSONArray();
        for(String businessKey: businessKeys) {
            // 单据对应的所有变量
            //businessKey,region,bizTypeCode,var
            String[] businessKeyVar = businessKey.split(",");
            JSONObject paramObj = new JSONObject();
            //paramObj.put("bizTypeCode","602");
            //paramObj.put("rgCode", "36");
            paramObj.put("bizTypeCode", businessKeyVar[2]);
            paramObj.put("rgCode", businessKeyVar[1]);
            paramObj.put("businessKey", businessKeyVar[0]);
            JSONArray varArray = new JSONArray();
            JSONObject var = new JSONObject();
            var.put("name","billtype_code");
            var.put("type","string");
            var.put("value", businessKeyVar[3]);
            varArray.add(var);
            //varArray.add(var2);
            //varArray.add(var3);
            paramObj.put("variables", varArray);
            paramArray.add(paramObj);

        }
        req.put("batchProcessInstanceCreateRequest", paramArray);

        return req;
    }


}
